const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const { port = 8080, proxyTable = [] } = require('./config.js');

const app = express();

proxyTable.forEach((item) => app.use(item.api, createProxyMiddleware({
  target: item.target, // 目标服务器 host
  changeOrigin: true, // // 默认false，是否需要改变原始主机头为目标URL
  ws: true // 是否代理websockets
})))
if(process.env.MODE === 'dev'){
  app.use('/ruoyi', createProxyMiddleware({target: 'http://localhost:8372/ruoyi/', changeOrigin: true, ws: true }))
  app.use('/backend', createProxyMiddleware({target: 'http://localhost:8372/backend/', changeOrigin: true, ws: true }))
  app.use('/fantastic/', createProxyMiddleware({target: 'http://localhost:8371/fantastic/', changeOrigin: true, ws: true }))
  app.use('', createProxyMiddleware({target: 'http://localhost:8371/fantastic/', changeOrigin: true, ws: true }))
} else {
  app.use('/', express.static('./dist')); // 设置静态资源访问路径
  app.get('/', (req, res) => {
    res.sendfile(`${__dirname}/dist/fantastic.html`)
  })
  app.get('/backend', (req, res) => {
    res.sendfile(`${__dirname}/dist/ruoyi.html`)
  })
}

app.listen(port,() => {
  console.log(`项目已启动：http://localhost:${port}`);
})